package org.BooCrack.bookstore.pay.controller.admin;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.BooCrack.bookstore.common.utils.Response;
import org.BooCrack.bookstore.common.utils.ResponseResult;
import org.BooCrack.bookstore.model.entity.Store;
import org.BooCrack.bookstore.model.entity.User;
import org.BooCrack.bookstore.model.service.IStoreService;
import org.BooCrack.bookstore.model.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;

import java.util.List;

@RestController
//@RequestMapping("/admin/store")
@RequestMapping("admin/V8")
@RequiresPermissions("store-manage")
@Api(value = "v1", tags = "8-6.后台管理系统管理管理商店模块接口")
public class StoreController {

    @Autowired
    private IStoreService storeService;

    @Autowired
    private IUserService userService;

    @Value("${business.role-id}")
    private String business;

    @GetMapping("/store")
    @RequiresPermissions("store-list")
    @ApiOperation(value = "商店列表", notes = "商店列表")
    public ResponseResult storeList(Model model){

        List<Store> stores = storeService.findStores();



        return Response.createOkResp(stores);
    }

    @GetMapping("/store/toAddition")
    @RequiresPermissions("store-add")
    @ApiOperation(value = "商店添加页", notes = "商店添加页")
    public ResponseResult addStore(Model model){
        List<User> users = userService.findBusinesses(Integer.parseInt(business));
        model.addAttribute("users", users);
        //return "admin/store/add";
        return Response.createOkResp(users);
    }


    @GetMapping("/store/{storeId}")
    @RequiresPermissions("store-edit")
    @ApiOperation(value = "根据id查看商店信息", notes = "根据id查看商店信息")
    public ResponseResult toEdit(@PathVariable("storeId") int storeId, Model model){

        Store store = storeService.findById(storeId);

        //model.addAttribute("store", store);

        //return "admin/store/edit";
        return Response.createOkResp(store);
    }

    @PutMapping("/store")
    @RequiresPermissions("store-edit")
    @ApiOperation(value = "商店信息修改", notes = "商店信息修改")
    public ResponseResult editStore(Store store,Model model){

        storeService.updateStore(store);

        //model.addAttribute("saveMsg", "保存成功");

        //return "forward:"+store.getStoreId();
        return  Response.createOkResp("保存成功！",store.getStoreId());
    }

    @DeleteMapping("/store/{storeId}")
    @RequiresPermissions("store-delete")
    @ApiOperation(value = "根据id删除商店信息", notes = "根据id删除商店信息")
    public ResponseResult deleteStore(@PathVariable("storeId") int storeId){

        try {
            storeService.deleteStore(storeId);
            //return "redirect:/admin/store/list";
            return Response.createOkResp("删除成功");
        }catch (Exception e){
            return Response.createFailResp("删除失败！");
        }
    }

    @PostMapping("/store")
    @RequiresPermissions("store-add")
    @ApiOperation(value = "新增商店", notes = "新增商店")
    public ResponseResult addStore(Store store){
        try {
            storeService.addStore(store);
            //return "redirect:/admin/store/list";
            return Response.createOkResp("更新成功！");

        }catch (Exception e){
            return Response.createFailResp("更新失败！");
        }

    }

}
